﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class clientManager : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            CSUtils.CheckSession();            
            this.binddataUserName();
            this.binddataState();

            string clientCode = Request.Params["clientCode"];
            if (clientCode != null && clientCode != "")
            {
                GridView1.DataSource = this.showdata(clientCode);
                GridView1.DataBind();                
            }
            else
            {
                this.binddata();
            }
        }
    }
    
    

    private void binddataUserName()
    {
        DropDownListUser.DataSource = CSUtils.getAllUserList("2");
        DropDownListUser.DataBind();

    }

    private void binddataState()
    {
        DropDownListState.DataSource = CSUtils.getAllState();
        DropDownListState.DataBind();
    }

    protected void DropDownListState_SelectedIndexChanged(object sender, EventArgs e)
    {
        string stateID = DropDownListState.SelectedValue;
        DropDownListCity.Items.Clear();
        DropDownListCity.Items.Add(new ListItem("请选择...", ""));
        if (stateID != "")
        {
            DropDownListCity.DataSource = CSUtils.getAllCity(stateID);
            DropDownListCity.DataBind();
        }
    }

    private void binddata()
    {
        GridView1.DataSource = this.getAllClient();
        GridView1.DataBind();
    }

    private DataSet showdata(string clientCode)
    {
        string sql =
            "select * " +
            "  from cs_client_view " +
            "  where client_code = '" + clientCode + "'";
        
        DataSet ds = new DataSet();
        DBConn myDB = new DBConn();
        ds = myDB.getDataSet(sql);
        myDB.Close();
        return ds;
    }

    private DataSet getAllClient()
    {

        string sql =
            "select * " +
            "  from cs_client_view " +
            "  where {0} " + 
            "  order by client_code, area, state, city";

        string where = "1=1";

        string clientName = TextBoxClient.Text;
        if (clientName.Trim() != "" && !clientName.StartsWith("输入"))
        {
            where += "and client_name ='" + clientName + "' ";
        }

        string userID = DropDownListUser.SelectedValue;
        if (userID != "-1")
        {
            where += "and user_id  = " + userID + " ";
        }

        if (DropDownListState.SelectedValue != "")
        {
            string state = DropDownListState.SelectedItem.Text;
            where += "and state ='" + state + "' ";
        }

        if (DropDownListCity.SelectedValue != "")
        {
            string city = DropDownListCity.SelectedItem.Text;
            where += "and city ='" + city + "' ";
        }

        string clientCode = TextBoxClientCode.Text;
        if (clientCode.Trim() != "" )
        {
            where += "and client_code ='" + clientCode + "' ";
        }

        string status = DropDownListClientStatus.SelectedValue;
        if (status != "-1")
        {
            where += "and client_status ='" + status + "' ";
        }

        //市场
        string market = DropDownListMarketType.SelectedValue;
        if (!market.Equals("-1"))
        {
            where += "and market_type ='" + market + "' ";
        }
        else
        {
            where += "and market_type <> '2' ";
        }

        sql = string.Format(sql, where);

        DataSet ds = new DataSet();
        DBConn myDB = new DBConn();
        ds = myDB.getDataSet(sql);
        myDB.Close();
        return ds;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {
        GridView1.PageIndex = e.NewPageIndex;
        this.binddata();
    }


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string clientID = e.CommandArgument.ToString();

        if (e.CommandName.Equals("command_visible"))
        {
            GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);   //获取被点击的linkButton所在的GridViewRow
            int index = gvrow.RowIndex;                                                         //获取到行索引 RowIndex

            //获取当前行的某列值
            string visible = GridView1.Rows[index].Cells[9].Text;
            if (visible == "True")
            {
                //如果客户当前余额不为0, 不能变为不可见
                double balance = CSUtils.getClientBalance(clientID);
                if (balance != 0)
                {
                    JScript.Alert("不能变为不可见， 客户当前余额为:[" + balance + "], 请先结清！");
                    return;
                }
            }

            if (this.updateClientVisible(clientID))
            {

            }
            else
            {
                JScript.Alert("保存失败！");
            }

            this.binddata();
        }
        else
        {

        }

    }


    protected void ButtonQuery_Click(object sender, EventArgs e)
    {
        binddata();
    }

    private bool updateClientVisible(string clientID)
    {

        string sqlUpdate =
            "update cs_client " +
            "set visible = visible-1 " +
            "where client_id = " + clientID;

        DBConn myDB = new DBConn();
        int tmp = myDB.Update(sqlUpdate);
        myDB.Close();
        return (tmp == 1);

    }

}
